package org.extframework.ld.util;

import java.util.ArrayList;
import java.util.List;

public class StringUtils {

	public static String substringBetween(String str, String open, String close) {
		if (str == null || open == null || close == null) {
			return null;
		}
		int start = str.indexOf(open);
		if (start != -1) {
			int end = str.indexOf(close, start + open.length());
			if (end != -1) {
				return str.substring(start + open.length(), end);
			}
		}
		return null;
	}

	public static String substringBetweenFirstAndLast(String str, String first,
			String last) {
		if (str == null || first == null || last == null) {
			return null;
		}
		int start = str.indexOf(first);
		if (start != -1) {
			int end = str.lastIndexOf(last);
			if (start == end) {
				return null;
			}
			if (end != -1) {
				return str.substring(start + 1, end);
			}
		}
		return null;
	}

	public static String replaceAll(String str, String repl, String with) {
		if (str == null || "".equals(str)) {
			return str;
		}
		while (str.indexOf(repl) != -1) {
			str = replace(str, repl, with);
		}
		return str;
	}

	public static String replace(String str, String repl, String with) {
		if (isEmpty(str)) {
			return str;
		}

		int replIndex = str.indexOf(repl);
		if (replIndex == -1) {
			return str;
		}
		String beforeRepl = str.substring(0, replIndex);
		String afterRepl = str.substring(replIndex + repl.length());
		return beforeRepl + with + afterRepl;
	}

	public static boolean isEmpty(String str) {
		return str == null || str.equals("");
	}

	public static String substringBefore(String str, String separator) {
		if (isEmpty(str) || separator == null) {
			return str;
		}
		if (separator.length() == 0) {
			return "";
		}
		int pos = str.indexOf(separator);
		if (pos == -1) {
			return str;
		}
		return str.substring(0, pos);
	}

	public static String substringBeforeLast(String str, String separator) {
		if (isEmpty(str) || separator == null) {
			return str;
		}
		if (separator.length() == 0) {
			return "";
		}
		int pos = str.lastIndexOf(separator);
		if (pos == -1) {
			return str;
		}
		return str.substring(0, pos);
	}

	public static String substringAfterLast(String str, String separator) {
		if (isEmpty(str)) {
			return str;
		}
		if (isEmpty(separator)) {
			return "";
		}
		int pos = str.lastIndexOf(separator);
		if (pos == -1 || pos == (str.length() - separator.length())) {
			return "";
		}
		return str.substring(pos + separator.length());
	}

	public static String substringAfter(String str, String separator) {
		if (isEmpty(str)) {
			return str;
		}
		if (isEmpty(separator)) {
			return "";
		}
		int pos = str.indexOf(separator);
		if (pos == -1) {
			return "";
		}
		return str.substring(pos + separator.length());
	}

	public static String[] split(String str, String separator) {
		List segs = new ArrayList();
		String seg = substringBefore(str, separator);
		while ((!isEmpty(str) && !isEmpty(seg) && !seg.equals(str))) {
			segs.add(seg);
			str = substringAfter(str, separator);
			seg = substringBefore(str, separator);
		}
		segs.add(seg);
		String[] result = new String[segs.size()];
		for (int i = 0, j = segs.size(); i < j; i++) {
			result[i] = (String) segs.get(i);
		}
		return result;
	}

	public static int compare(String s1, String s2) {
		int n1 = s1.length(), n2 = s2.length();
		for (int i1 = 0, i2 = 0; i1 < n1 && i2 < n2; i1++, i2++) {
			char c1 = s1.charAt(i1);
			char c2 = s2.charAt(i2);
			if (c1 != c2) {
				c1 = Character.toUpperCase(c1);
				c2 = Character.toUpperCase(c2);
				if (c1 != c2) {
					c1 = Character.toLowerCase(c1);
					c2 = Character.toLowerCase(c2);
					if (c1 != c2) {
						return c1 - c2;
					}
				}
			}
		}
		return n1 - n2;
	}

}
